package blocstac.editimage.zoom
{
	import blocstac.editimage.EditState;
	import blocstac.editimage.base.StateManager;
	import blocstac.editimage.rotate.RotateComponent;
	
	import flash.events.MouseEvent;
	
	import mx.core.UIComponent;
	
	import spark.components.HSlider;

public class ZoomComponent extends HSlider
{
	private var _stateManager:StateManager
	private var _backgroundComponent:UIComponent
	
	function ZoomComponent(stateManager:StateManager, backgroundComponent:UIComponent) {
		_stateManager = stateManager
		_backgroundComponent = backgroundComponent
	}
	
	//--------------------------------------------------------------------------
	//
	//  Methods
	//
	//--------------------------------------------------------------------------
	override protected function createChildren():void {
		minimum = 100
		maximum = 400
		value = 100
		stepSize = 20
		snapInterval = 10
		setStyle("liveDragging", true)
		width= 100
		height = 11
		 
		dataTipPrecision = 0
		dataTipFormatFunction = dataTipFormatter

		super.createChildren()
	}
	
	/**
	 * 
	 * @param minimum 1 corresponds to a 100% of the image
	 */
	public function setMinimumAndValueTo(minimum:Number):void {
		var newMinimum:Number = Math.floor(minimum * 100)
		if (newMinimum != this.minimum) {
			this.minimum = newMinimum
			this.value = newMinimum
		}
	}
	
	private function dataTipFormatter(value:Number):Object { 
		return value + "%" 
	}
	
	public function updateEditState():void {
		visible = false
		if (_stateManager.editState.isZoomable) {
			value = _stateManager.viewModel.userViewScale * 100
			_backgroundComponent.addEventListener(MouseEvent.ROLL_OVER, rollOverHandler, false, 0, true) 
		} else {
			_backgroundComponent.removeEventListener(MouseEvent.ROLL_OVER, rollOverHandler)
			_backgroundComponent.removeEventListener(MouseEvent.ROLL_OUT, rollOutHandler)
		}
	}
	
	private function rollOverHandler(event:MouseEvent):void {
		visible = true
		_backgroundComponent.removeEventListener(MouseEvent.ROLL_OVER, rollOverHandler)
		_backgroundComponent.addEventListener(MouseEvent.ROLL_OUT, rollOutHandler, false, 0, true)
	}
	
	private function rollOutHandler(event:MouseEvent):void {
		visible = false
		_backgroundComponent.removeEventListener(MouseEvent.ROLL_OUT, rollOutHandler)
		_backgroundComponent.addEventListener(MouseEvent.ROLL_OVER, rollOverHandler, false, 0, true) 
	}

}
}